package sn.gandal.mobile.kalpet.server;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import sn.gandal.mobile.kalpet.data.entities.Account;
import sn.gandal.mobile.kalpet.data.entities.DatabaseManager;



public class ServerResourcesManager implements IServerResourcesManager {
	
	static private ServerResourcesManager instance;
	public static final String URL_GUICHET="http://192.168.0.30:8080/guichet_mobile/kmobile";
	public static final String GUICHET_RESOURCES="/guichetResources";

    static public void init() {
        if (null==instance) {
            instance = new ServerResourcesManager();
        }
    }

    static public ServerResourcesManager getInstance() {
        return instance;
    }

	@Override
	public Account getAccountById(String id) {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public void saveAccount(Account account) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void saveCompteCredit() {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void saveCompteCarte() {
		// TODO Auto-generated method stub
		
	}


	@Override
	public String authServer(String codeSecret, String numeroCli) throws ClientProtocolException, IOException {
		String url= URL_GUICHET+GUICHET_RESOURCES;
		 HttpClient httpclient = new DefaultHttpClient();
    	 HttpGet request=new HttpGet(url);
    	 request.setHeader("content-type", "application/JSON");
    	    HttpResponse response = httpclient.execute(request);
    	    StatusLine statusLine = response.getStatusLine();
    	    String responseString =null;
    	    if(statusLine.getStatusCode()== HttpStatus.SC_OK){
    	        ByteArrayOutputStream out = new ByteArrayOutputStream();
    	        response.getEntity().writeTo(out);
    	        out.close();
    	        responseString = out.toString();
    	        //..more logic
    	    } else{
    	        //Closes the connection.
    	        response.getEntity().getContent().close();
    	        throw new IOException(statusLine.getReasonPhrase());
    	    }
    	
    	return responseString;
	}


}
